package com.example.main;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class ThreeSecriteActivity extends FragmentActivity {

    private Button btn_sec;

    private static final String KEY_VALUE = "mcl20200";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sec);
        btn_sec = findViewById(R.id.btn_sec);
        btn_sec.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                logic();
            }
        });
    }

    private void logic()  {
        String value = encryptString("dingjiahui");
        decrypt(value);
    }

    private void decrypt(String value) {
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("DES");
            Key key2 = new SecretKeySpec(KEY_VALUE.getBytes(), "DES");
            //解密
            //3，设置操作模式（加密/解密）
            cipher.init(Cipher.DECRYPT_MODE, key2);
            //4，执行解密
            //先用Base64解密 缺少Base64编码会导致解密失败
            byte[] decode = Base64.decode(value, Base64.DEFAULT);
            byte[] bytes = cipher.doFinal(decode);
            String decodeStr = new String(bytes);
            Log.d("xl decrypt:", decodeStr);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }

    }

    /**
     * 加密
     *
     * @param value
     */
    private String encryptString(@NonNull String value) {
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("DES");
            Key key1 = new SecretKeySpec(KEY_VALUE.getBytes(), "DES");
            //加密
            //3，设置操作模式（加密/解密）
            cipher.init(Cipher.ENCRYPT_MODE, key1);
            //4，执行加密
            byte[] relBytes = cipher.doFinal(value.getBytes());
            //注意：加密过后用Base64编码 缺少这步会导致解密失败
            byte[] relBase = Base64.encode(relBytes, Base64.DEFAULT);
            String encodeStr = new String(relBase);
            Log.d("xl encryptString:", encodeStr);
            return encodeStr;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }

        return null;
    }
}
